package cn.yesway.module.customer.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.yesway.common.Json;
import cn.yesway.module.customer.service.CustomerService;
import cn.yesway.module.customer.vo.Customer;

@Controller
@RequestMapping("/customer/*")
public class CustomerController {

	private final Logger log = LoggerFactory.getLogger(CustomerController.class);

	@Autowired
	CustomerService customerService;

	@RequestMapping("index.do")
	public String index(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		return "index";
	}

	@RequestMapping("query.do")
	public @ResponseBody List<Customer> getAll(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		log.info("getAll start name=" + name);
		List<Customer> l = new ArrayList<Customer>();
		if (StringUtils.isEmpty(name)) {
			l = customerService.getAll();
		}else{
			l = customerService.getByCondition(name);
		}
		return l;
	}
	
	@RequestMapping("getByid.do")
	public @ResponseBody Customer getByid(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String id = request.getParameter("id");
		log.info("getByid start id=" + id);
		return customerService.getById(id);
	}

	@RequestMapping("delete.do")
	public @ResponseBody Json delete(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String id = request.getParameter("id");
		Json j = new Json();
		j.setObj(null);
		if (customerService.deleteCustomer(id) > 0) {
			j.setSuccess(true);
			j.setMsg("删除成功");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		return "editCustomer";
	}
	
	@RequestMapping("save.do")
	public @ResponseBody Json save(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Json j = new Json();
		j.setObj(null);
		String customer = request.getParameter("customer");
		JSONObject json = JSONObject.fromObject(customer);
		Customer c = (Customer) JSONObject.toBean(json, Customer.class);
		if (StringUtils.isEmpty(c.getId())) {
			log.info("save  insert  c=" + c.toString());
			customerService.addCustomer(c);
			j.setMsg("添加用户成功");
			j.setSuccess(true);
		} else {
			log.info("save  update  c=" + c.toString());
			customerService.updateCustomer(c);
			j.setMsg("更新用户成功");
			j.setSuccess(true);
		}
		return j;
	}
}
